<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('LACONICA')) { exit(1); }

/**
 * Table Definition for profile
 */
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';

class Custom_theme extends Memcached_DataObject
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    public $__table = 'custom_theme';                         // table name
    public $id;                                 // int(4)  primary_key not_null
    public $body_color;                        // varchar(23)  
    public $body_type;                        // tinyint(4)  
    public $body_image;                        // varchar(255)  
    public $body_image_header;                        // varchar(255)  
    public $body_image_footer;                        // varchar(255) 
    public $body_position;                        // varchar(32) 
    public $body_fixed;                        // tinyint(4)    
    public $body_text_color;                        // varchar(32)      
    public $body_link_color;                        // varchar(32) 
    public $content_color;                        // varchar(32) 
    public $content_opacity;                        // varchar(32) 
    public $content_text_color;                        // varchar(32)
    public $content_link_color;                        // varchar(32)
    public $tab_background_color;                        // varchar(32)
    public $tab_border_color;                        // varchar(32)
    public $tab_text_color;                        // varchar(32)
    public $tab_current_color;                        // varchar(32)
    public $sidebar_border_color;                        // varchar(32)
    public $sidebar_color;                        // varchar(32)
    public $sidebar_text_color;                        // varchar(32)
    public $sidebar_opacity;                        // varchar(32)
    public $sidebar_link_color;                        // varchar(32)
    public $site_theme;                                 // tinyint(4)
    public $content_hover;      

    /* Static get */
    function staticGet($k,$v=null)
    { return Memcached_DataObject::staticGet('Custom_theme',$k,$v); }
    
    function deleteMainImage()
    {
        $filename = $this->body_image;
        if(!is_null($filename) && !empty($filename))
        {
            @unlink(Custom_theme::path($filename));
        }
    }
    function deleteHeaderImage()
    {
        $filename = $this->body_image_header;
        if(!is_null($filename) && !empty($filename))
        {
            @unlink(Custom_theme::path($filename));
        }
    }
    function deleteFooterImage()
    {
        $filename = $this->body_image_footer;
        if(!is_null($filename) && !empty($filename))
        {
            @unlink(Custom_theme::path($filename));
        }
    }
    
    static function path($filename)
    {
        return INSTALLDIR . '/usertheme/' . $filename;
    }
    
    static function url($filename)
    {
        return common_path('usertheme/'.$filename);
    }
    
    static function filename($id, $extension, $size=null, $extra=null)
    {
        if ($size) {
            return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
        } else {
            return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
        }
    }
    static function filename2($id, $extension, $size=null, $extra=null)
    {
        if ($size) {
            return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
        } else {
            return $id . '-spec' . (($extra) ? ('-' . $extra) : '') . $extension;
        }
    }

    
    function showBody()
    {
        $bodyvalue='html, body {';
        $overall='';
        $mainlink='';
      //  $htmlvalue='html {';
        if(!is_null($this->body_color) && !empty($this->body_color))
        {
            $bodyvalue.='background-color :'.$this->body_color.';';
           // $htmlvalue.='background-color :'.$this->body_color.'; }';
        }
        if(!is_null($this->body_text_color) && !empty($this->body_text_color))
        {
            $bodyvalue.='color :'.$this->body_text_color.';';
        }
        if($this->body_type==1)
        {
          if(!is_null($this->body_image) && !empty($this->body_image))
                    {

                        $bodyvalue.='background-image:url('.$this->url($this->body_image).'); background-repeat:';
                                               
                        switch ($this->body_position)
                        {
                            case '0':
                            $bodyvalue.='repeat;';   
                            break;
                            case '1':
                            $bodyvalue.='no-repeat; background-position:top left;';   
                            break;
                            case '2':
                            $bodyvalue.='no-repeat; background-position:top center;';   
                            break;
                            case '3':
                            $bodyvalue.='no-repeat; background-position:top right;';   
                            break;
                            case '4':
                            $bodyvalue.='no-repeat; background-position:center left;';   
                            break;
                            case '5':
                            $bodyvalue.='no-repeat; background-position:center center;';   
                            break;
                            case '6':
                            $bodyvalue.='no-repeat; background-position:center right;';   
                            break;
                            case '7':
                            $bodyvalue.='no-repeat; background-position:bottom left;';   
                            break;
                            case '8':
                            $bodyvalue.='no-repeat; background-position:bottom center;';   
                            break;
                            case '9':
                            $bodyvalue.='no-repeat; background-position:bottom right;';   
                            break;     
                        }
                         if(!is_null($this->body_fixed) && !empty($this->body_fixed))
                         {
                              $bodyvalue.='background-attachment:fixed;';   
                         }
                        
                    }
        } else if($this->body_type==2)
        {
                    if(!is_null($this->body_image_footer) && !empty($this->body_image_footer))
                    {
                         $bodyvalue.='background-image:url('.$this->url($this->body_image_footer).'); background-repeat:no-repeat; background-position:bottom left; ';
                    
                    }
                    if(!is_null($this->body_image_header) && !empty($this->body_image_header))
                    {
                         $overall.='#overall { background-image:url('.$this->url($this->body_image_header).'); background-repeat:no-repeat; background-position:top left; } ';
                    
                    }
        }
        
        if(!is_null($this->body_link_color) && !empty($this->body_link_color))
        {
            $mainlink.=' body a{ color :'.$this->body_link_color.'; }';
        }
        
        $bodyvalue.='} ';
        $bodyvalue.=$overall;
        $bodyvalue.=$mainlink;
       // $bodyvalue.=$htmlvalue;
        
        return $bodyvalue;
        
    }
    function showContent()
    {
        $contentvalue='#content { ';
        if(!is_null($this->content_color) && !empty($this->content_color))
        {
            $contentvalue.='background-color :'.$this->content_color.';';
        }
        if(!is_null($this->content_opacity) && !empty($this->content_opacity))
        {
            $contentvalue.='opacity :'.$this->content_opacity.';';
        }
        if(!is_null($this->content_text_color) && !empty($this->content_text_color))
        {
            $contentvalue.='color :'.$this->content_text_color.';';
        }
        
        $contentvalue.='} ';
        
        if(!is_null($this->content_link_color) && !empty($this->content_link_color))
        {
             $contentvalue.='#content a{ color:'.$this->content_link_color.'; }';
        }

        if(!is_null($this->content_hover) && !empty($this->content_hover))
        {
             $contentvalue.='.notices li.hover { background-color:'.$this->content_hover.'; }';
        }
        
        return $contentvalue;
    }
    function showTab()
    {
        $tabvalue='#site_nav_local_views a {';
        if(!is_null($this->tab_background_color) && !empty($this->tab_background_color))
        {
             $tabvalue.='background-color :'.$this->tab_background_color.';';
        }
        if(!is_null($this->tab_border_color) && !empty($this->tab_border_color))
        {
             $tabvalue.='border-color :'.$this->tab_border_color.';';
        }
        if(!is_null($this->tab_text_color) && !empty($this->tab_text_color))
        {
             $tabvalue.='color :'.$this->tab_text_color.';';
        }
        $tabvalue.='} ';
        
        if(!is_null($this->tab_current_color) && !empty($this->tab_current_color))
        {
             $tabvalue.='#site_nav_local_views .current a { background-color:'.$this->tab_current_color.'; }';
        }
        
        return $tabvalue;

        
    
    }
    function showSidebar()
    {
         $sidebarvalue='#aside_primary { ';
        if(!is_null($this->sidebar_color) && !empty($this->sidebar_color))
        {
            $sidebarvalue.='background-color :'.$this->sidebar_color.';';
        }
        if(!is_null($this->sidebar_border_color) && !empty($this->sidebar_border_color))
        {
            $sidebarvalue.='border-color :'.$this->sidebar_border_color.';';
        }
        if(!is_null($this->sidebar_opacity) && !empty($this->sidebar_opacity))
        {
            $sidebarvalue.='opacity :'.$this->sidebar_opacity.';';
        }
        if(!is_null($this->sidebar_text_color) && !empty($this->sidebar_text_color))
        {
            $sidebarvalue.='color :'.$this->sidebar_text_color.';';
        }
         $sidebarvalue.='} ';
         
         if(!is_null($this->sidebar_link_color) && !empty($this->sidebar_link_color))
        {
              $sidebarvalue.='#aside_primary a{ color:'.$this->sidebar_link_color.'; }';
        }
        
        return  $sidebarvalue;
    }
    
    function showAll()
    {
        $theme='<style type="text/css">';
        $theme.=$this->showBody();
        $theme.=$this->showContent();
        $theme.=$this->showTab();
        $theme.=$this->showSidebar();
        $theme.='</style>';
        return $theme;
    }
    

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE

    
   
}
